/**
 * @author Linrui
 * @since 2011-05-16
 * 提供首页访问的产品查询action,不需要登录
 */
package com.qeado.product.web;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.qeado.product.model.Product;
import com.qeado.product.service.ProductService;
import com.qeado.util.PageHelper;

@Scope("session")
@Controller
public class ProductIndexAction extends ActionSupport {

	@Autowired
	private ProductService productService;
	
	private Product product;
	private PageHelper productList;
	
	private String pageNum = "1";
	private String pageSize = "10";
	private String typeCode; // 产品类别
	private String queryType; // 信息类别
	private String orderBy = "create_date"; // 默认按发布时间来排列
	private String asc = "desc"; // 默认是逆序

	public String getTypeCode() {
		return typeCode;
	}

	public void setTypeCode(String typeCode) {
		this.typeCode = typeCode;
	}

	public String getQueryType() {
		return queryType;
	}

	public void setQueryType(String queryType) {
		this.queryType = queryType;
	}

	public PageHelper getProductList() {
		return productList;
	}

	public void setProductList(PageHelper productList) {
		this.productList = productList;
	}

	public String getPageNum() {
		return pageNum;
	}

	public void setPageNum(String pageNum) {
		this.pageNum = pageNum;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getAsc() {
		return asc;
	}

	public void setAsc(String asc) {
		this.asc = asc;
	}
	
	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}
	
	public String getSingleProduct() throws Exception{
		String id = ServletActionContext.getRequest().getParameter("id") ;
		product = productService.getProductById(id);
		if( product != null )
			return "view";
		else
			return "visit_fail";		
	}
	
	/* 按关键字搜索产品 */
	public String search() throws Exception {
		String key = ServletActionContext.getRequest().getParameter("key");
		productList = productService.listByKey(Integer.parseInt(pageNum), Integer.parseInt(pageSize), key, orderBy, asc);
		productList.setPagebar("${ctx}/product/search?key="+key);
		return "list";
	}
	
	/**
	 * 按分类分页查询产品
	 */
	public String getListBySort() throws Exception{
		productList = productService.listBySort(Integer.parseInt(pageNum), Integer.parseInt(pageSize), typeCode, orderBy, asc, null);
		return "list";
	}
}
